/*
 * @Author: your name
 * @Date: 2021-11-13 15:40:01
 * @LastEditTime: 2021-12-05 21:12:35
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\controller\tagDb.js
 */
const Mock = require("mockjs");
const { list } = Mock.mock({
    "list|20": [{
        desc: "@ctitle",
        title: "@ctitle",
        "state|1": [false, true],
        "is_allow_comment|1": [false, true],
        cover: "@image(400x160,@color,@name)",
    }, ],
});
const fs = require("fs");
const path = require("path");
const pump = require("pump");
const Controller = require("../core/base_controller");

class KnowledgeController extends Controller {
    async insertList() {
            const { ctx, app } = this;
            list.forEach((item) => {
                app.mysql.insert("knowledge", {...item });
            });
            ctx.body = "添加成功";
        }
        //获取数据
    async getKnowledgeList() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.getKnowledgeList();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //添加数据
    async addKnowledgeList() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.addKnowledgeList();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //删除数据
    async delKnowledgeList() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.delKnowledgeList();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //修改数据
    async editKnowledgeList() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.editKnowledgeList();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //改变线上状态
    async changeKnowledgeState() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.changeKnowledgeState();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //上传图片
    async fileSetLoad() {
            const { ctx } = this;
            // 1.获取由前端传来的文件信息
            const stream = await ctx.getFileStream();
            // 2。生成随机的文件名称  toLocaleLowerCase:将大写字母转换为小写字母
            const filename =
                Date.now() + path.extname(stream.filename).toLocaleLowerCase();
            // 3,创建文件的保存地址
            const target = path.join("app/public/knowledge", filename);
            // 4,生成一个文件流
            const writeStream = fs.createWriteStream(target);
            // 5,将文件信息通过流,流入文件地址中
            await pump(stream, writeStream);
            // 6. 获取图片的服务器地址
            const imgUrl = ctx.origin + "/public/knowledge/" + filename;
            ctx.body = {
                code: 200,
                url: imgUrl,
            };
        }
        //获取详情（一级）
    async getKnowledgeDetail() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.getKnowledgeDetail();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //获取二级内容
    async getKnowledgeChildren() {
            const { ctx } = this;
            try {
                const res = await ctx.service.knowledge.getKnowledgeChildren();
                this.success(res);
            } catch (err) {
                this.error(err.message);
            }
        }
        //     //获取二级内容
    async addKnowledgeChildren() {
        const { ctx } = this;
        try {
            const res = await ctx.service.knowledge.addKnowledgeChildren();
            this.success(res);
        } catch (err) {
            this.error(err.message);
        }
    }
}

module.exports = KnowledgeController;